package com.LeeCode;

import java.util.Arrays;

/**
 * 最长和谐子序列
 */

public class Code594 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};
        System.out.println(new Code594().findLHS(nums));
    }

    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int left = 0, ans = 0;
        for (int right = 0; right < nums.length; right++) {

            while (nums[right] - nums[left] > 1) {
                left++;
            }
            if (nums[right] - nums[left] == 1)
                ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
